package com.shop2cn.iapi.couponmanage.testcase;

import java.text.SimpleDateFormat;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.common.collect.Lists;
import com.shop2cn.iapi.coupon.parameter.UseCouponBean;
import com.shop2cn.iapi.couponmanage.parameter.SellerCreateBatchBean;
import com.shop2cn.iapi.couponmanage.parameter.getBatchInfosBean;
import com.shop2cn.iapi.couponmanage.parameter.old.BatchInfo;
import com.shop2cn.iapi.couponmanage.service.SellerCreateBatchCall;
import com.shop2cn.iapi.couponmanage.service.getBatchInfosCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponShop2cnCallService;
import com.ymttest.business.service.CouponShop2cnMCallService;
import com.ymttest.database.model.CouponShop2cn.couponbatch;
import com.ymttest.database.model.CouponShop2cn.couponusecondition;
import com.ymttest.database.sqlwapper.CouponShop2cn.couponbatchWapper;
import com.ymttest.database.sqlwapper.CouponShop2cn.couponuseconditionWapper;
import com.ymttest.utils.TestDataManager;
/**
*根据优惠券编号获取优惠券信息
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_getBatchInfos{
	private static getBatchInfosBean bean;
	private static getBatchInfosCall call;
	
	private static SellerCreateBatchBean sellercreatebatchBean=new SellerCreateBatchBean();;
	private static SellerCreateBatchCall sellercreatebatchCall=new SellerCreateBatchCall(); 
	
	private couponbatchWapper couponbatchWapper=new couponbatchWapper();
	
	CouponShop2cnMCallService couponShop2cnMCallService=new CouponShop2cnMCallService();
	CouponShop2cnCallService couponShop2cnCallService=new CouponShop2cnCallService();
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据优惠券编号获取优惠券信息");
	}

	@Before
	public void caseUp() {
		bean=new getBatchInfosBean();
		call=new getBatchInfosCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getBatchInfos_001() {
		Logger.start(true,"根据优惠券编号获取优惠券信息-正常查询-返回成功");
		try {
			sellercreatebatchBean = CouponShop2cnMCallService.setDefaultSellerCreateBatchBean();
			sellercreatebatchCall =CouponShop2cnMCallService.SellerCreateBatch(sellercreatebatchBean);
			couponbatch batch=couponbatchWapper.selectBybatchCode(sellercreatebatchCall.getBatchCode()).get(0);
			List<couponbatch> list = new couponbatchWapper().selectBybatchCode(sellercreatebatchCall.getBatchCode());
			bean.setBatchCodes(Lists.newArrayList(batch.getBatchCode()));
			call.setData(bean);
			call.callService();			
			
			VerifyReturn(list.get(0));
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getBatchInfos_002() {
		Logger.start(true,"根据优惠券编号获取优惠券信息-batchCodes中含不存在的-返回成功");
		try {
			
			bean.setBatchCodes(Lists.newArrayList("11111"));
			
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("0", call.getCode(),"验证返回code");
		    Logger.verifyEquals("操作成功！",call.getMessage(), "验证返回Message");
		    Logger.verifyEquals(0,call.getBatchInfo().size(), "验证返回Message");
	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getBatchInfos_003() {
		Logger.start(true,"根据优惠券编号获取优惠券信息-allowDelayReceivedCount=false-领取-返回成功");
		try {
			Integer buyUserId=TestDataManager.getRandomIntNum(5);
			sellercreatebatchBean = CouponShop2cnMCallService.setDefaultSellerCreateBatchBean();
			sellercreatebatchCall =CouponShop2cnMCallService.SellerCreateBatch(sellercreatebatchBean);
			couponbatch batch=couponbatchWapper.selectBybatchCode(sellercreatebatchCall.getBatchCode()).get(0);
			
			//领取
			String couponCode1=couponShop2cnCallService.receiveCouponInfo(sellercreatebatchCall.getBatchCode(), buyUserId);
			
			List<couponbatch> list = new couponbatchWapper().selectBybatchCode(sellercreatebatchCall.getBatchCode());
			
	        bean.setBatchCodes(Lists.newArrayList(batch.getBatchCode()));
	      //  bean.setAllowDelayReceivedCount(false);
			
			call.setData(bean);
			call.callService();			
			
			VerifyReturn(list.get(0));
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getBatchInfos_004() {
		Logger.start(true,"根据优惠券编号获取优惠券信息-allowDelayReceivedCount=true-领取-返回成功");
		try {
			Integer buyUserId=TestDataManager.getRandomIntNum(5);
			sellercreatebatchBean = CouponShop2cnMCallService.setDefaultSellerCreateBatchBean();
			sellercreatebatchCall =CouponShop2cnMCallService.SellerCreateBatch(sellercreatebatchBean);
			couponbatch batch=couponbatchWapper.selectBybatchCode(sellercreatebatchCall.getBatchCode()).get(0);
			
			//领取
			String couponCode1=couponShop2cnCallService.receiveCouponInfo(sellercreatebatchCall.getBatchCode(), buyUserId);
			
			List<couponbatch> list = new couponbatchWapper().selectBybatchCode(sellercreatebatchCall.getBatchCode());
	        bean.setBatchCodes(Lists.newArrayList(batch.getBatchCode()));
	        bean.setAllowDelayReceivedCount(true);
			
			call.setData(bean);
			call.callService();			
			
			VerifyReturn(list.get(0));
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getBatchInfos_005() {
		Logger.start(true,"根据优惠券编号获取优惠券信息-allowDelayUsedCounto=false-使用-返回成功");
		try {
			Integer buyUserId=TestDataManager.getRandomIntNum(5);
			sellercreatebatchBean = CouponShop2cnMCallService.setDefaultSellerCreateBatchBean();
			sellercreatebatchCall =CouponShop2cnMCallService.SellerCreateBatch(sellercreatebatchBean);
			couponbatch batch=couponbatchWapper.selectBybatchCode(sellercreatebatchCall.getBatchCode()).get(0);

			//领取
			String couponCode1=couponShop2cnCallService.receiveCouponInfo(sellercreatebatchCall.getBatchCode(), buyUserId);
			//使用
			UseCouponBean useCouponBean=couponShop2cnCallService.setUseCouponBean(buyUserId, TestDataManager.getRandomIntNum(5),couponCode1);
			couponShop2cnCallService.useCoupon(useCouponBean);
			
			List<couponbatch> list = new couponbatchWapper().selectBybatchCode(sellercreatebatchCall.getBatchCode());
			
	        bean.setBatchCodes(Lists.newArrayList(batch.getBatchCode()));
	        bean.setAllowDelayUsedCounto(false);
			
			call.setData(bean);
			call.callService();			
			
			VerifyReturn(list.get(0));	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getBatchInfos_006() {
		Logger.start(true,"根据优惠券编号获取优惠券信息-allowDelayUsedCounto=true-返回成功");
		try {
			Integer buyUserId=TestDataManager.getRandomIntNum(5);
			sellercreatebatchBean = CouponShop2cnMCallService.setDefaultSellerCreateBatchBean();
			sellercreatebatchCall =CouponShop2cnMCallService.SellerCreateBatch(sellercreatebatchBean);
			couponbatch batch=couponbatchWapper.selectBybatchCode(sellercreatebatchCall.getBatchCode()).get(0);

			//领取
			String couponCode1=couponShop2cnCallService.receiveCouponInfo(sellercreatebatchCall.getBatchCode(), buyUserId);
			//使用
			UseCouponBean useCouponBean=couponShop2cnCallService.setUseCouponBean(buyUserId, TestDataManager.getRandomIntNum(5),couponCode1);
			couponShop2cnCallService.useCoupon(useCouponBean);
			
			List<couponbatch> list = new couponbatchWapper().selectBybatchCode(sellercreatebatchCall.getBatchCode());
			
	        bean.setBatchCodes(Lists.newArrayList(batch.getBatchCode()));
	        bean.setAllowDelayUsedCounto(true);
			
			call.setData(bean);
			call.callService();			
			
			VerifyReturn(list.get(0));		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	private void VerifyReturn(couponbatch item) {
		
		List<BatchInfo> batchInfos = call.getBatchInfo();
		for(int i=0;i<batchInfos.size();i++)
		{
			BatchInfo batchInfo=batchInfos.get(i);
			checkCouponBatch(item,batchInfo);
		    //验证使用条件
		  // checkcouponusecondition(item.getBatchId(),batchInfo);
		}

	}
	private void checkCouponBatch(couponbatch item,BatchInfo batchInfo)
	{
		 Logger.comment("====方法：验证数据库checkcouponbatch====");
	        Logger.verifyEquals("0", call.getCode(),
	                "验证返回code");
	     
	        Logger.verifyEquals("操作成功！",
	        		call.getMessage(), "验证返回Message");


	 
	         String batchCode = batchInfo.getBatchCode();
	         Logger.verifyEquals(item.getBatchId(),
	        		batchInfo.getBatchId(),
	                "验证返回BatchCode与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
	                        + "记录中iBatchId字段相同");
	        
//	        Logger.verifyEquals(item.getUseConditionType(), batchInfo.getUseConditionType(),
//					"验证返回字段productLimitType与Ymt_CouponSetting表中UseConditionType一致");
	        
	        Logger.verifyEquals(item.getBatchCode(),
	        		batchInfo.getBatchCode(),
	                "验证返回BatchCode与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
	                        + "记录中sBatchCode字段相同");
	        
	        // 验证AcquireStartTime，AcquireEndTime，AcquireCouponMethod
	        SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:ms"); 
	        if(item.getAcquireStartTime()!=null&&batchInfo.getAcquireStartTime()!=null)
	        {
	        	String acquireStartTime =time.format(item.getAcquireStartTime());
	        	Logger.verifyEquals(acquireStartTime.substring(0, 19).replace(" ", "T"),
	        		batchInfo.getAcquireStartTime(),
	                "验证返回BatchCode与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
	                        + "记录中AcquireStartTime字段相同");
	        }
	        if(item.getAcquireEndTime()!=null&&batchInfo.getAcquireEndTime()!=null)
	        {
	        	String acquireEndTime =time.format(item.getAcquireEndTime());
	        	Logger.verifyEquals(acquireEndTime.substring(0, 19).replace(" ", "T"),
	        		batchInfo.getAcquireEndTime(),
	                "验证返回BatchCode与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
	                        + "记录中AcquireEndTime字段相同");
	        }
	        // 是否作废
	        if(item.getIsInvalid()==null)
	        {
	        	Logger.verifyEquals(false,
	            		batchInfo.getIsExpire(),
	                    "验证返回IsCancellation与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
	                            + "记录中bInvalidStatus字段相同");
	        }
	        else {
	        	Logger.verifyEquals(item.getIsInvalid(),
	            		batchInfo.getIsExpire(),
	                    "验证返回IsCancellation与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
	                            + "记录中IsInvalid字段相同");
			}
	        
	     // 是否领完
	        Logger.verifyEquals(item.getTotalNum()==item.getReceivedCount(),
	        		batchInfo.getReceiveOver(),
	                "验证返回IsReceiveOver正确");
	           
	   
//	        Logger.verifyEquals(item.getSendType(),
//	        		batchInfo.getSendType(),
//	                "验证返回SendType与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
//	                        + "记录中iSendType字段相同");
	        
	        if(item.getEffectiveType()==null)
	        {
	        	Logger.verifyEquals(0,
	        		batchInfo.getEffectiveType(),
	                "验证返回EffectiveType与数据库中表CouponBatch中iEffectiveType字段相同");
	        }
	        else{
	        	   Logger.verifyEquals(item.getEffectiveType(),
	               		batchInfo.getEffectiveType(),
	                       "验证返回EffectiveType与数据库中表CouponBatch中iEffectiveType字段相同");
	        }
	        
	        if(item.getEffectiveValidType()==null)
	        {
	        	 Logger.verifyEquals(0,
	             		batchInfo.getEffectiveValidType(),
	                     "验证返回EffectiveValidType与数据库中表CouponBatch中iEffectiveValidType字段相同");
	             }
	        else
	        {
	        	 Logger.verifyEquals(item.getEffectiveValidType(),
	             		batchInfo.getEffectiveValidType(),
	                     "验证返回EffectiveValidType与数据库中表Ymt_CouponSetting中iEffectiveValidType字段相同");
	        
	        }

	        Logger.verifyEquals(time.format(item.getAcquireStartTime()).subSequence(0, 19),
	        		batchInfo.getAcquireStartTime().replace("T", " "),
	                "验证返回ValidStart与数据库中表couponbatch中acquireStartTime字段相同");

	        Logger.verifyEquals(time.format(item.getAcquireEndTime()).subSequence(0, 19),
	        		batchInfo.getAcquireEndTime().replace("T", " "),
	                "验证返回ValidEnd与数据库中表couponbatch中acquireEndTime字段相同");
	        
	        if(item.getEffectiveDays()==null)
	        {
	        	Logger.verifyEquals(0,
	        		batchInfo.getEffectiveDays(),
	                "验证返回iEffectiveDays与数据库中表couponbatch中iEffectiveDays字段相同");
	        }
	        else {
	        	Logger.verifyEquals(item.getEffectiveDays().intValue(),
	            		batchInfo.getEffectiveDays(),
	                    "验证返回iEffectiveDays与数据库中表couponbatch中iEffectiveDays字段相同");     
			}
	        
	        Logger.verifyEquals(item.getTotalNum(),
	        		batchInfo.getCouponCount(),
	                "验证返回TotalCount与数据库中表couponbatch中CouponCount字段相同");
	        
	        Logger.verifyEquals(item.getReceivedCount(),
	        		batchInfo.getReceivedCount(),
	                "验证返回BatchCode与数据库中表couponbatch中ReceivedCount字段相同");
	        
//	        if(item.getIsShowInPage()==null)
//	        {
//	        	Logger.verifyEquals(false,
//	        		batchInfo.getIsShowInPage(),
//	                "验证返回BatchCode与数据库中表couponbatch中sBatchCode=" + batchCode
//	                        + "记录中IsShowInPage字段相同");
//	        }
//	        else {
//	            Logger.verifyEquals(item.getIsShowInPage(),
//	            		batchInfo.getIsShowInPage(),
//	                    "验证返回BatchCode与数据库中表couponbatch中sBatchCode=" + batchCode
//	                            + "记录中IsShowInPage字段相同");
//			}
	        
	        Logger.verifyEquals(item.getReceiveUserType(),
	        		batchInfo.getUserType(),
	                "验证返回BatchCode与数据库中表couponbatch中iUserType字段相同");
	        // 验证coupontype-分享券
	     //   int couponType=item.getAcquireMethod()==3?2:1;
	        Logger.verifyEquals((byte)item.getCouponType(),
	        		batchInfo.getCouponType(),
	                 "验证返回BatchCode与数据库中表Ymt_CouponBatch中coupontype字段相同");
	        
	        //活动Id
	        Logger.verifyEquals(item.getCouponTypeBizValue(),
	        		batchInfo.getCouponTypeBizValue(),
	                "验证返回BatchCode与数据库中表couponbatch中sBatchCode=" + batchCode
	                        + "记录中CouponTypeBizValue字段相同");
	        
	        Logger.verifyEquals(item.getEffectiveTimeType(), batchInfo.getEffectiveTimeType(), 
	        		"验证返回EffectiveTimeType与数据库中couponbatch中sBatchCode=" + batchCode
	        		+"记录中EffectiveTimeType字段相同");
	        
	        
	        
	        Logger.verifyEquals(item.getReceivedCount(), batchInfo.getReceivedCount(),
						"验证返回字段ReceiveNumPerUser与couponbatch表中ReceivedCount一致");
	        
//	        //币种
//	        Logger.verifyEquals(item.getCurrency(), batchInfo.getCurrency(), 
//	        		"验证返回Currency与数据库中couponbatch中sBatchCode=" + batchCode
//	        		+"记录中Currency字段相同");
	        
	}
//	/**
//	 * 
//	 * @param batchId
//	 * @param item 数据库查询couponbatch
//	 * @param batchInfo call返回的batchInfo
//	 */
//	 private void checkcouponusecondition(int batchId,BatchInfo callbatchInfo)
//	    {
//	    	 Logger.comment("====方法：验证数据库checkcouponusecondition====");
//	    	 Logger.comment("########## couponusecondition 限制条件表 #############");
//				couponuseconditionWapper userWapper=new couponuseconditionWapper();
//				List<couponusecondition> userLst=userWapper.selectByBatchId(batchId);
//				
//				couponbatch couponbatch= new couponbatchWapper().selectByPrimaryKey(batchId);
//				
////				if(callbatchInfo.getUseConditionType()==1)
////				{
////					Logger.verifyEquals(1, userLst.size(), "验证数据库couponusecondition表中的BatchId="+batchId+"的记录中的条数");
////					Logger.verifyEquals(1,userLst.get(0).getConditionType(),
////							"验证数据库couponusecondition表中BatchId="+ batchId+ "的记录中的ConditionValue()");
////					Logger.verifyEquals(couponbatch.getOperatorId().toString(),userLst.get(0).getConditionValue(),
////							"验证数据库couponusecondition表中BatchId="+ batchId+ "的记录中的ConditionValue()");
////				}	
////				else if(callbatchInfo.getUseConditionType()==2)
////				{
////					Logger.verifyEquals(true, userLst.size()>1, "验证数据库couponusecondition表中的BatchId="+batchId+"的记录中的条数最少2条");
////					for(int i=0;i<userLst.size();i++)
////					{
////						if(userLst.get(i).getConditionType()==1)
////						Logger.verifyEquals(couponbatch.getOperatorId().toString(),userLst.get(i).getConditionValue(),
////								"验证数据库couponusecondition表中BatchId="+ batchId+ "的记录中的ConditionValue()");
////						else if(userLst.get(i).getConditionType()==2)
////						{
////							if(sellercreatebatchBean.getproductIdList()!=null && sellercreatebatchBean.getproductIdList().size()>0)
////							Logger.verifyEquals(true,sellercreatebatchBean.getproductIdList().contains(userLst.get(i).getConditionValue()),
////									"验证数据库couponusecondition表中BatchId="+ batchId+ "的记录中的ConditionValue()");
////						}
////					}
////				}
//	    }
	}